from pydantic import BaseModel
from fastapi import Body, FastAPI
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
from fastapi import FastAPI, Request
from fastapi import FastAPI
from cpm import do_predict
app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}


app.mount("/static", StaticFiles(directory="static"), name="static")


templates = Jinja2Templates(directory="templates")


@app.get("/demo", response_class=HTMLResponse)
async def index(request: Request):
    return templates.TemplateResponse("index.html", {"request": request, "id": id})

@app.get("/echart", response_class=HTMLResponse)
async def index(request: Request):
    return templates.TemplateResponse("echart.html", {"request": request, "id": id})


class Item(BaseModel):
    src_txt: str
    char_num_dst: int
    num_dst: int


@app.post("/predict")
async def predict(request: Request, item: Item = Body(..., embed=True)):
    results = do_predict(
        src_txt=item.src_txt, char_num_dst=item.char_num_dst, num_dst=item.num_dst)

    # results = {"src_txt": src_txt, "item": item}
    return results

